package com.seeyon.apps.reimburseplugin.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seeyon.apps.reimburseplugin.dao.ReimburseDao;
import com.seeyon.apps.reimburseplugin.po.Reimbursement;
import com.seeyon.apps.reimburseplugin.po.ReimbursementSon;
import com.seeyon.apps.reimburseplugin.po.TemporaryPO;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItemPO;
import com.seeyon.ctp.common.po.filemanager.Attachment;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.organization.po.OrgLevel;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgPost;
import com.seeyon.ctp.organization.po.OrgPrincipal;
import com.seeyon.ctp.organization.po.OrgUnit;
import com.seeyon.ctp.util.DBAgent;
import com.seeyon.ctp.util.JDBCAgent;

public class ReimburseDaoImpl implements ReimburseDao{

	@Override
	public TemporaryPO findReimburseById(Long id) {
		Map<String,Object> params=new HashMap<String,Object>();
		String sql=" from TemporaryPO where ID=:id";
		params.put("id", id);
		List<TemporaryPO> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	
	@SuppressWarnings({ "deprecation", "resource", "unused" })
	public boolean saveReimbursement(Reimbursement reimbursement) {
		String sql="insert into reimbursement (ID,START_DEPT,EVECTION_REASON,EVECTION_PERSON,POST,TOTAL_TRAVEL_COSTS,TOTAL_MEALS_DAYS,TOTAL_MEALS_STANDARD,TOTAL_MEALS_NUMBER,TOTAL_MEALS_AMOUNT,TOTAL_TRAVELCOSTS_DAYS,TOTAL_TRAVELCOSTS_STANDARD,TOTAL_TRAVELCOSTS_NUMBER,TOTAL_TRAVELCOSTS_AMOUNT,TOTAL_HOTEL_EXPENSE,TOTAL_OTHER_EXPENSE,TOTAL_SUBTOTAL,TOTAL_MEETING_FEES,TOTAL,TOTAL_CAPITAL,AMOUNT,DATES,BUDGET_ITEM,BUDGET_DEPT,BUDGET_NATURE,FUNDS_NATURE,PK_GROUP,TAX_AMOUNT,GLOBALBBJE,ORGTAX_AMOUNT,SXBZ,BZBM,PAYFLAG,OPERATOR,PK_ORG,VAT_AMOUNT,PK_FIORG,BBJE,FYDWBM,GROUPBBJE,TNI_AMOUNT,DJDL,SPZT,DJBH,RECEIVER,JSFS,ORGTNI_AMOUNT,DJRQ,PAYTARGET,PK_PAYORG,DJZT,DJLXBM,DWBM,ORGVAT_AMOUNT,PK_ORG_V,YBJE,PK_BILLTYPE,CREATE_TIME,RES1,RES2)"
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> params=new ArrayList<Object>();
		params.add(reimbursement.getId());
		params.add(reimbursement.getStartDept());
		params.add(reimbursement.getEvectionReason());
		params.add(reimbursement.getEvectionPerson());
		params.add(reimbursement.getPost());
		params.add(reimbursement.getTotalTravelCosts());
		params.add(reimbursement.getTotalMealsDays());
		params.add(reimbursement.getTotalMealsStandard());
		params.add(reimbursement.getTotalMealsNumber());
		params.add(reimbursement.getTotalMealsAmount());
		params.add(reimbursement.getTotalTravelcostsDays());
		params.add(reimbursement.getTotalTravelcostsStandard());
		params.add(reimbursement.getTotalTravelcostsNumber());
		params.add(reimbursement.getTotalTravelcostsAmount());
		params.add(reimbursement.getTotalHotelExpense());
		params.add(reimbursement.getTotalOtherExpense());
		params.add(reimbursement.getTotalSubtotal());
		params.add(reimbursement.getTotalMeetingFees());
		params.add(reimbursement.getTotal());
		params.add(reimbursement.getTotalCapital());
		params.add(reimbursement.getAmount());
		params.add(reimbursement.getDates());
		params.add(reimbursement.getBudgetItem());
		params.add(reimbursement.getBudgetDept());
		params.add(reimbursement.getBudgetNature());
		params.add(reimbursement.getFundsNature());
		params.add(reimbursement.getPkGroup());
		params.add(reimbursement.getTaxAmount());
		params.add(reimbursement.getGlobalbbje());
		params.add(reimbursement.getOrgtaxAmount());
		params.add(reimbursement.getSxbz());
		params.add(reimbursement.getBzbm());
		params.add(reimbursement.getPayflag());
		params.add(reimbursement.getOperator());
		params.add(reimbursement.getPkOrg());
		params.add(reimbursement.getVatAmount());
		params.add(reimbursement.getPkFiorg());
		params.add(reimbursement.getBbje());
		params.add(reimbursement.getFydwbm());
		params.add(reimbursement.getGroupBbje());
		params.add(reimbursement.getTniAmount());
		params.add(reimbursement.getDjdl());
		params.add(reimbursement.getSpzt());
		params.add(reimbursement.getDjbh());
		params.add(reimbursement.getReceiver());
		params.add(reimbursement.getJsfs());
		params.add(reimbursement.getOrgtniAmount());
		params.add(reimbursement.getDjrq());
		params.add(reimbursement.getPayTarget());
		params.add(reimbursement.getPkPayorg());
		params.add(reimbursement.getDjzt());
		params.add(reimbursement.getDjlxbm());
		params.add(reimbursement.getDwbm());
		params.add(reimbursement.getOrgvatAmount());
		params.add(reimbursement.getPkOrgv());
		params.add(reimbursement.getYbje());
		params.add(reimbursement.getPkBilltype());
		params.add(reimbursement.getCreateTime());
		params.add(reimbursement.getFlag());
		params.add(reimbursement.getAttachments());
		int res=0;
		JDBCAgent jdbc=new JDBCAgent();
		try {
			jdbc.batch1Prepare(sql);
			jdbc.batch2Add(params);
			res=jdbc.batch3Execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (res!=0) {
			return true;
		}else {
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<OrgUnit> findOrgUnitByAccountId(Long accountId) {
		Map<String, Object> params=new HashMap<String, Object>();
		String sql=" from OrgUnit where id=:accountId";
		params.put("accountId", accountId);
		return DBAgent.find(sql,params);
	}
	@Override
	public OrgMember findMemberById(Long id) {
		Map<String, Object> params=new HashMap<String, Object>();
		String sql=" from OrgMember where id=:id and state=1 and is_deleted=0";
		params.put("id", id);
		List<OrgMember> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			OrgMember orgMember=list.get(0);
			return orgMember;
		}
		return null;
	}
	public OrgPrincipal findOrgPrincipalByMemId(String memberId) {
		Map<String, Object> params=new HashMap<String, Object>();
		String sql=" from OrgPrincipal where MEMBER_ID=:memberId";
		params.put("memberId", memberId);
		List<OrgPrincipal> list=DBAgent.find(sql,params);
		if (list!=null&&list.size()>0) {
			OrgPrincipal orgPrincipal=list.get(0);
			return orgPrincipal;
		}
		return null;
	}

	@Override
	public OrgPost findPostById(Long id) {
		Map<String, Object> params=new HashMap<String, Object>();
		String sql=" from OrgPost where id=:id";
		params.put("id", id);
		List<OrgPost> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			OrgPost orgPost=list.get(0);
			return orgPost;
		}
		return null;
	}


	@Override
	public CtpEnumItemPO findEnumById(Long id) {
		Map<String, Object> params=new HashMap<String, Object>();
		String sql=" from CtpEnumItemPO where id=:id";
		params.put("id", id);
		List<CtpEnumItemPO> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			CtpEnumItemPO ctpEnumItem=list.get(0);
			return ctpEnumItem;
		}	
		return null;
	}


	@Override
	public CtpAffair findAffairByObjId(String objId) {
		Map<String,Object> params=new HashMap<String,Object>();
		String sql=" from CtpAffair where OBJECT_ID=:objId and STATE=4 order by COMPLETE_TIME desc";
		params.put("objId", objId);
		List<CtpAffair> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}


	@Override
	public CtpTemplate findTemplate(String templateId) {
		Map<String,Object> params=new HashMap<String,Object>();
		String sql=" from CtpTemplate where ID=:templateId";
		params.put("templateId", templateId);
		List<CtpTemplate> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}


	@Override
	public List<Attachment> attachmentList(String attReferenceId) {
		Map<String,Object> params=new HashMap<String,Object>();
		String sql=" from Attachment where att_reference=:attReferenceId";
		params.put("attReferenceId", attReferenceId);
		List<Attachment> list=DBAgent.find(sql, params);
		if (list!=null&&list.size()>0) {
			return list;
		}
		return null;
	}
}
